const matchModel = require('../models/matches.js')

class MatchCtrl {

    async list() {
        let lst = await matchModel.find().sort('id');
        return lst.map(m=>m.toJSON());
    }

    async create(matchData) {
        let M = new matchModel(matchData);
        M = await M.save();
        return M.toJSON();
    }

    async retrieve(id) {
        let M = await matchModel.findById(id);
        return M?M.toJSON(): null;
    }

    async upsert(id, matchData) {
        matchData.id = id;
        let M = await matchModel.upsert(id, matchData);
        return M.toJSON();
    }
    async removeById(id) {
        await matchModel.removeById(id);
        return 1;
    }
    async find(filter)
    {
        let r = await matchModel.find(filter);
        return r.map(e=>e.toJSON());
    } 
}

module.exports = new MatchCtrl();